/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Lock;
import except.StoreException;
import java.io.File;
import java.util.List;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.Singleton;
//import java.util.concurrent.ReadWriteLock;
import javax.annotation.PreDestroy;
import com.sleepycat.dbxml.XmlContainer;
import com.sleepycat.dbxml.XmlDocument;
import com.sleepycat.dbxml.XmlException;
import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlQueryContext;
import com.sleepycat.dbxml.XmlQueryExpression;
import com.sleepycat.dbxml.XmlResults;
import com.sleepycat.dbxml.XmlValue;
import java.util.ArrayList;

/**
 *
 * @author Peetrr
 */
@Singleton
@Lock(javax.ejb.LockType.READ)
@ConcurrencyManagement(javax.ejb.ConcurrencyManagementType.CONTAINER)
public class StoreBean implements Store {
    private String container;
    private XmlContainer myContainer;
    private XmlManager myManager;

    @PostConstruct
    @Override
    public void initialize(String container) throws StoreException {
        if (container == null) {
            throw new StoreException("Initialization exception: Null container name not allowed.");
        } else {
            this.container = container;
            try {
                this.myManager = new XmlManager();
            } catch (XmlException ex) {
                Logger.getLogger(StoreBean.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(StoreBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                myContainer = myManager.openContainer(container);
            } catch (XmlException ex) {
                Logger.getLogger(StoreBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        // 1. Create the database table if it does not exist.
        // 2. Initialize settings from the database table.
        // 3. Load a cache.
        // 4. Initiate asynchronous work (for example, work to a messaging queue or to
        //    calls to asynchronous session bean methods.

    }

    @PreDestroy
    @Override
    public void destroy() {
        try {
            // ... if col is opened, close it
            myContainer.close();
        } catch (XmlException ex) {
            Logger.getLogger(StoreBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("closed");
    }

    @Override
    @Lock(javax.ejb.LockType.WRITE)
    public void add(File file) throws StoreException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    @Lock(javax.ejb.LockType.WRITE)
    public void remove(String title) throws StoreException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<String> search(String title) throws StoreException {
        List<String> contents;
        contents = new ArrayList<String>();
        if (title == null) {
            throw new StoreException("Title should not be empty");
        } else {
            try {
                String myQuery = "for $a in collection('" + container + "') "
                        + "where " + "contains($a//*/@id,'" + title + "')"
                        + " return dbxml:metadata('dbxml:name', $a)";
                System.out.println(myQuery);
                XmlQueryContext context = myManager.createQueryContext();
                XmlQueryExpression qe = myManager.prepare(myQuery, context);
                XmlResults results = qe.execute(context);
                XmlValue value = results.next();
                while (value != null) {
                    String docString = value.asString();
                    contents.add(docString);
                    value = results.next();
                }


                
            } catch (XmlException ex) {
                Logger.getLogger(StoreBean.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                return contents;
            }
        }
    }

    @Override
    public List<String> searchAnnot(String title) throws StoreException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getContents() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public File download(String title) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public File download(String title, String param) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    // ...
}
